﻿#ifndef _SELECTSERVER_H
#define _SELECTSERVER_H

#include <map>
using namespace std;
#include "server.h"
#include "fdset.h"

namespace SocketLib
{
    // select 模型服务器
    template<typename _SOCKETSTRUCT>
    class SelectServer : public BaseServer
    {
        public:
        typedef _SOCKETSTRUCT SOCKETSTRUCT;
        public:

        SelectServer(int num = 1024);

        bool    poll(long aOverTime);

        int     getClientNum();
        SOCKETSTRUCT*   getSocketBuffer();


        private:
        void    _handlerRecv();
        void    _handlerSend();

        void    _handlerAcceptList();
        void    _handlerCloseList();

        private:
        void    _auxHandlerNewClient(sock);
        bool    _handlerClientRecv(SOCKETSTRUCT*);
        void    _disConnection(SOCKETSTRUCT*);

        private:

        // 客户端链接资源
        TypePool<SOCKETSTRUCT>  m_socketPoll;

        SocketSet               m_socketSet;

        // 访问m_socketSet select的结果
        fd_set*                 m_readSetResult;
        fd_set*                 m_writeSetResult;

        typedef    map<sock, SOCKETSTRUCT*> ACTIVELIST;
        // 已经(建立链接)分配的资源
        ACTIVELIST              m_connectedSockets;
    };
}

#include "../src/selectserver.cpp"

#endif
